var fs = require('fs')
var path = require('path')
var sd = require('silly-datetime')
var dbPath = path.join(__dirname, '../data/students.json')
var connection = require('../modules/connect')

//获取学生列表
exports.find = function (callback) {
    connection.query('select * from `students`', function (err, data) {
        if (err) { return callback(err) }
        return callback(null, data)
    })
}

//获取编辑信息
exports.findById = function (id, callback) {
    connection.query("select * from students where id=" + id + ";", function (err, data) {
        if (err) { return callback(err) }
        return callback(null, data)
    })
}

exports.save = function (student, callback) {
    if (!student.name) {
        return callback(null, 1)
    } else if (!student.hobbies) {
        return callback(null, 2)
    } else if (!student.age) {
        return callback(null, 3)
    } else if (isNaN(student.gender)) {
        return callback(null, 4)
    } else if (student.name.length < 2 || student.name.length > 10) {
        return callback(null, 5)
    } else if (isNaN(student.age)) {
        return callback(null, 6)
    } else if (parseInt(student.age) <= 0 || parseInt(student.age) >= 150 || student.age == '') {
        return callback(null, 7)
    } else if (student.hobbies.length < 4 || student.hobbies.length > 100) {
        return callback(null, 8)
    } else {
        var time = sd.format(new Date(), 'YYYY-MM-DD HH:mm')
        connection.query("insert into `students` values(null,'" + student.name + "'," + student.gender + "," + student.age + ",'" + student.hobbies + "','" + time + "');", function (err) {
            if (err) { return callback(err) }
            return callback(null, 0)
        })
    }
}

/**
 * 更新学生
 */
exports.updateById = function (student, callback) {
    if (!student.name) {
        return callback(null, 1)
    } else if (!student.hobbies) {
        return callback(null, 2)
    } else if (!student.age) {
        return callback(null, 3)
    } else if (isNaN(student.gender)) {
        return callback(null, 4)
    } else if (student.name.length < 2 || student.name.length > 10) {
        return callback(null, 5)
    } else if (isNaN(student.age)) {
        return callback(null, 6)
    } else if (parseInt(student.age) <= 0 || parseInt(student.age) >= 150 || student.age == '') {
        return callback(null, 7)
    } else if (student.hobbies.length < 4 || student.hobbies.length > 100) {
        return callback(null, 8)
    } else {
        connection.query("update `students` set name='" + student.name + "',gender=" + student.gender + ",age=" + student.age + ",hobbies='" + student.hobbies + "' where id =" + student.id + ";", function (err) {
            if (err) { return callback(err) }
            return callback(null, 0)
        })
    }
}

/**
 * 删除学生
 */
exports.deleteById = function (ids, callback) {

    if (Array.isArray(ids)) {
        var sql = `delete from students where id in (`; //用来拼接？
        param = [];
        for (let i = 0; i < ids.length; i++) { //循环传过来的id数组，插入到param变量数组中
            param.push(ids[i])
        }
        for (let i = 0; i < ids.length - 1; i++) { //循环拼接sql
            sql = sql + ids[i] + `,`;
        }
        sql = sql + ids[ids.length - 1] + `)`;
    } else {
        sql = `delete from students where id =${ids}`
    }
    connection.query(sql, function (err) {
        if (err) { return callback(err) }
        return callback(null)
    })
}
